package com.example.controller;

import com.example.Utils.DingText;
import com.example.pojo.*;
import com.example.service.precisionMeasProjectService;
import com.example.service.suspectProductListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:precisionMeasProjectController
 * Description:
 *
 * @Author 赵永峰
 * @Create 2024/12/29 16:17
 * @Version 1.0
 */
@RestController
@Slf4j
public class precisionMeasProjectController {
    //注入控制层
    @Autowired
    private precisionMeasProjectService precisionMeasProjectService;
    @Autowired
    private suspectProductListService suspectProductListService;

    //插入一行检测项目数据
    @PostMapping("/precisionMeasProjectInsertOne")
    public Result precisionMeasProjectInsertOne(@RequestBody precisionMeasProjectPojo precisionMeasProjectPojo){
        //
        precisionMeasProjectService.insertOne(precisionMeasProjectPojo);
        return Result.success("插入成功");
    }
    //输入三个字段，进行查询
    @PostMapping("/precisionMeasProjectInquiryThreeField")
    public Result precisionMeasProjectInquiryThreeField(@RequestBody Map<String,Object> map){
        //返回一个list
        List<precisionMeasProjectPojo> precisionMeasProjectPojoList = precisionMeasProjectService.inquiryThreeField(map);
        return Result.success(precisionMeasProjectPojoList);
    }
    //输入四个字段，进行查询
    @PostMapping("/precisionMeasProjectInquiryFourField")
    public Result precisionMeasProjectInquiryFourthField(@RequestBody Map<String,Object> map){
        //返回一个list
        List<precisionMeasProjectPojo> precisionMeasProjectPojoList = precisionMeasProjectService.inquiryFourthField(map);
        return Result.success(precisionMeasProjectPojoList);
    }
    //根据id，修改数据，一整行都要修改
    @PostMapping("/precisionMeasProjectEditOne")
    public Result precisionMeasProjectEditOne(@RequestBody precisionMeasProjectPojo precisionMeasProjectPojo){
        precisionMeasProjectService.precisionMeasProjectEditOne(precisionMeasProjectPojo);
        return Result.success("保存成功");
    }
    //修改一个字段.依据id
    @PostMapping("/precisionMeasProjectEditOneField")
    public Result precisionMeasProjectEditOneField(@RequestBody Map<String,Object> map){
        precisionMeasProjectService.precisionMeasProjectEditOneField(map);
        return Result.success("删除成功");
    }
    //排序插入
    @PostMapping("/precisionMeasProjectSortInsert")
    public Result precisionMeasProjectSortInsert(@RequestBody List<precisionMeasProjectPojo> precisionMeasProjectPojoList){
        for (int i = 0; i < precisionMeasProjectPojoList.size(); i++) {
            if (precisionMeasProjectPojoList.get(i).getId()==null){
                //执行插入一行的代码
                precisionMeasProjectService.insertOne(precisionMeasProjectPojoList.get(i));
            }
            else {
                //执行修改一个字段，修改的是顺序号
                Map<String, Object> map = new HashMap<>();
                map.put("id", precisionMeasProjectPojoList.get(i).getId());
                map.put("firstFieldName", "orderNumber");
                map.put("firstFieldVal", precisionMeasProjectPojoList.get(i).getOrder_number());
                precisionMeasProjectService.precisionMeasProjectEditOneField(map);
            }
        }
        return Result.success("保存成功");
    }

    //————————————————————————————————————————————————————————————————————————————————————————————————————
    //接收到检测数据，对数据进行保存
    @PostMapping("/testValPrecisionMeasProject")
    public Result testValPrecisionMeasProject(@RequestBody List<precisionMeasResultPojo> precisionMeasResultPojos){
        //保存数据
        precisionMeasProjectService.testValPrecisionMeasProject(precisionMeasResultPojos);
        return Result.success("保存成功");
    }
    //接收5个参数，查询出检测结果。
    @PostMapping("/precisionMeasProjectValInquiryFiveField")
    public Result precisionMeasProjectValInquiryFiveField(@RequestBody Map<String,Object> map){
        List<precisionMeasResultPojo> precisionMeasResultPojos = precisionMeasProjectService.precisionMeasProjectValInquiryFiveField(map);
        return Result.success(precisionMeasResultPojos);
    }
    //批量的插入检测标准数据
    @PostMapping("/insertTableDataListPrecisionMeasProject")
    public Result insertTableDataListPrecisionMeasProject(@RequestBody List<precisionMeasProjectPojo> pojos){
        precisionMeasProjectService.insertTableDataList(pojos);
        System.out.println(pojos);
        return Result.success("插入成功");
    }

    //查询检测结果表的某字段为某值的数据，分页查询
    @PostMapping("/queryPrecisionMeasurementValueFourFieldMacTestCon")
    public Result queryPrecisionMeasurementValueOneFieldMacTestCon(@RequestBody Map<String,Object> map){
        List<precisionMeasResultPojo>pojoList = precisionMeasProjectService.queryFourField(map);
        return Result.success(pojoList);
    }
    //查询检测结果总条数
    @PostMapping("/queryPrecisionMeasurementValueQuantityMacTestCon")
    public Result queryPrecisionMeasurementValueQuantityMacTestCon(@RequestBody Map<String,Object> map){
        Integer quantity = precisionMeasProjectService.queryQuantity(map);
        return Result.success(quantity);
    }

    //动态查询检测结果
    @PostMapping("/queryPrecisionMeasurementValueByDynamicConditionsMacTestCon")
    public Result queryPrecisionMeasurementValueByDynamicConditionsMacTestCon(@RequestBody Map<String,Object> map){
        Integer page = (Integer) map.get("page");
        Integer size = (Integer) map.get("size");
        System.out.println("接收到的分页参数 - page: " + page + ", size: " + size);
        List<precisionMeasResultPojo> precisionMeasResultPojos = precisionMeasProjectService.queryByDynamicConditions(map);

        // 同时返回总数
        Integer total = precisionMeasProjectService.queryCountByDynamicConditions(map);

        Map<String, Object> result = new HashMap<>();
        result.put("data", precisionMeasResultPojos);
        result.put("total", total);

        return Result.success(result);
    }

    //编辑某一行测量结果
    @PostMapping("/editMeasuringValueMacTestCon")
    public Result editMeasuringValueMacTestCon(@RequestBody precisionMeasResultPojo precisionMeasResultPojo){
        precisionMeasProjectService.editOneRow(precisionMeasResultPojo);
        return Result.success("保存成功");
    }

    //根据检测项目查询指定数量的数据
    @PostMapping("/queryMeasuringProjectByOneFieldMacTestCon")
    public Result queryMeasuringProjectByOneFieldMacTestCon(@RequestBody Map<String,Object> map){
        List<precisionMeasResultPojo>pojoList = precisionMeasProjectService.queryByOneField(map);
        return Result.success(pojoList);
    }

    //如果检测结果中有不合格项，向可疑品清单中插入一行
    @PostMapping("/insertOneSuspectProductListMacTestCon")
    public Result insertOneSuspectProductListMacTestCon(@RequestBody suspectProductListPojo pojo) {
        //在可疑品数据表中插入一条新的数据或修改的数据，数据类型是suspectProductListPojo 数据是pojo
        suspectProductListService.insertOne(pojo);

        // 解析 describe_use
        String describeUse = pojo.getDescribe_use();
        String[] parts = describeUse.split(",");
        // 提取 Project 部分并替换 |*| 为 /
        String projectPart = parts[0].replace("Project:", "");
        String[] projectLanguages = projectPart.split("\\|\\*\\|");
        String formattedProject = String.join("/", projectLanguages);
        // 提取 Requirement 部分并按 |*| 分割后取第一部分
        String requirementPart = parts[1].replace("Requirement:", "");
        String[] requirements = requirementPart.split("\\|\\*\\|");
        String firstRequirement = requirements[0]; // 取第一个
        // 提取 Value 部分
        String valuePart = parts[2].replace("Value:", "").replace(";", "");
        //组合发送信息的内容
        String message = "❌ Part name**：" + pojo.getPart_name() + "\n" +
                "---------------------------------------\n" +  // 分隔线
                "❌ Part number**：" + pojo.getPart_id() + "\n" +
                "---------------------------------------\n" +  // 分隔线
                "❌ Project**：" + formattedProject + "\n" +
                "---------------------------------------\n" +  // 分隔线
                "❌ Requirement**：" + firstRequirement + "\n" +
                "---------------------------------------\n" +  // 分隔线
                "❌ Value**：" + valuePart;
        //向钉钉群发送信息
        List<String> list=new ArrayList<>();
        System.out.println(message);
        //如果想@人的话在这里
        list.add("GW00151667");//朱耀东
        list.add("GW00022732");//柴龙雨
        DingText.sendMessageByText(
                message,
                list,
                false,
                //机器人的识别号
                "SECd2d3f64a11f98aaa8612ba175b607d6ab7fc8eebe0547e7e8df4663ac1cdd20c",
                //机器人的地址号
                "https://oapi.dingtalk.com/robot/send?access_token=0833cbaa0d3d17d81109daf423b1da5898656bfc4bc6b3e7cc642774750a5908");
        return Result.success("可疑品插入成功");
    }

    //调整检测项目顺序
    @PostMapping("/editProjectOrderMacTestCon")
    public Result editProjectOrderMacTestCon(@RequestBody List<precisionMeasProjectPojo> pojo){
        for (int i = 0; i < pojo.size(); i++) {
            Map<String,Object> map=new HashMap<>();
            map.put("firstFieldName","order_number");
            map.put("firstFieldVal",pojo.get(i).getOrder_number());
            map.put("id",pojo.get(i).getId());
            precisionMeasProjectService.precisionMeasProjectEditOneField(map);
        }
        return Result.success(pojo);
    }
}
